
<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="平台" prop="platformId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.platformId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="渠道" prop="channelId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.channelId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="账单类型" prop="type">
          <el-select clearable @keyup.enter="handleQuery" v-model="queryParams.type" placeholder="请选择" style="width: 256px">
            <el-option :value="1" label="总账单" />
            <el-option :value="2" label="供应商账单" />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.supplierId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in supplierOptions" :key="item.id" :label="item.companyName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商编号" prop="supplierNo">
          <el-input style="width: 256px" v-model="queryParams.supplierNo" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="账单时间" prop="billingTime">
          <el-date-picker style="width: 256px" v-model="queryParams.billingTime" type="month" format="YYYYMM" valueFormat="YYYYMM" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <div class="upload_box">
          <el-button type="primary" preIcon="ant-design:export-outlined" @click="handleExports">导出</el-button>
          <el-upload
            v-hasPerm="['show/export/btn']"
            class="upload-demo"
            :show-file-list="false"
            :auto-upload="false"
            :before-upload="handleBeforeUpload"
            @change="handleChange($event, '罚款导入')"
          >
            <el-button type="primary">罚款导入</el-button>
          </el-upload>
          <el-upload
            v-hasPerm="['show/export/btn']"
            class="upload-demo"
            :show-file-list="false"
            :auto-upload="false"
            :before-upload="handleBeforeUpload"
            @change="handleChange($event, '扣款导入')"
          >
            <el-button type="primary">扣款导入</el-button>
          </el-upload>
          <el-upload
            v-hasPerm="['show/export/btn']"
            class="upload-demo"
            :show-file-list="false"
            :auto-upload="false"
            :before-upload="handleBeforeUpload"
            @change="handleChange($event, '补偿导入')"
          >
            <el-button type="primary">补偿导入</el-button>
          </el-upload>
          <el-upload
            v-hasPerm="['show/export/btn']"
            class="upload-demo"
            :show-file-list="false"
            :auto-upload="false"
            :before-upload="handleBeforeUpload"
            @change="handleChange($event, '作废导入')"
          >
            <el-button type="primary">作废导入</el-button>
          </el-upload>
          <el-upload
            v-hasPerm="['show/export/btn']"
            class="upload-demo"
            :show-file-list="false"
            :auto-upload="false"
            :before-upload="handleBeforeUpload"
            @change="handleChange($event, '供应商导入')"
          >
            <el-button type="primary">供应商补偿导入</el-button>
          </el-upload>
          <a
            v-hasPerm="['show/export/btn']"
            style="color: blue; margin-right: 14px"
            href="https://gb-yyds.oss-cn-hangzhou.aliyuncs.com/%E8%AE%A2%E5%8D%95%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx"
          >
            导入模版
          </a>
          <a
            v-hasPerm="['show/export/btn']"
            style="color: blue"
            href="https://gb-yyds.oss-cn-hangzhou.aliyuncs.com/%E4%BE%9B%E5%BA%94%E5%95%86%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx"
          >
            供应商导入模版
          </a>
        </div>
      </template>
      <el-table v-loading="loading" :data="TableData" highlight-current-row row-key="id" @row-click="handleRowClick">
        <el-table-column label="账单类型" min-width="120" align="center" prop="type">
          <template #default="scope">
            <div v-if="scope.row.type == 1">总账单</div>
            <div v-else-if="scope.row.type == 2">供应商账单</div>
          </template>
        </el-table-column>
        <el-table-column label="平台" min-width="120" align="center" prop="platformId">
          <template #default="scope">
            <div>{{ platFormEnum[scope.row.platformId] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="渠道" min-width="120" align="center" prop="channelId">
          <template #default="scope">
            <div>{{ channelEnum[scope.row.channelId] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="供应商" min-width="200" align="center" prop="supplierName">
          <template #default="scope">
            <div>{{ scope.row.supplierNo }}-{{ scope.row.supplierName }}</div>
          </template>
        </el-table-column>
        <el-table-column label="完单量" min-width="120" align="center" prop="num"></el-table-column>
        <el-table-column label="总金额" min-width="120" align="center" prop="totalFund"></el-table-column>
        <el-table-column label="贵宾金额" min-width="120" align="center" prop="gbFund"></el-table-column>
        <el-table-column label="平台金额" min-width="120" align="center" prop="platformFund"></el-table-column>
        <el-table-column label="渠道金额" min-width="120" align="center" prop="channelFund"></el-table-column>
        <el-table-column label="司机金额" min-width="120" align="center" prop="driverFund"></el-table-column>
        <el-table-column label="司机已提现金额" min-width="120" align="center" prop="driverWithdrawFund"></el-table-column>
        <el-table-column label="司机未提现金额" min-width="120" align="center" prop="driverRemainFund"></el-table-column>
        <el-table-column label="供应商金额" min-width="120" align="center" prop="supplierFund"></el-table-column>
        <el-table-column label="供应商结算金额" min-width="120" align="center" prop="supplierBillFund"></el-table-column>
        <el-table-column label="罚款金额" min-width="120" align="center" prop="fineFund"></el-table-column>
        <el-table-column label="扣款金额" min-width="120" align="center" prop="cutFund"></el-table-column>
        <el-table-column label="补偿金额" min-width="120" align="center" prop="compensationFund"></el-table-column>
        <el-table-column label="账单时间" min-width="120" align="center" prop="billingTime"></el-table-column>
        <el-table-column label="创建时间" min-width="120" align="center" prop="createTime"></el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="220">
          <template #default="scope">
            <div>
              <!-- <el-button type="primary" link size="small" @click.stop="handleDetail(scope.row)">
                <template #icon><Pointer /></template>
                明细
              </el-button> -->
              <el-button type="primary" link size="small" @click.stop="handleExprotItem(scope.row)">
                <template #icon><Pointer /></template>
                导出
              </el-button>
              <el-button v-if="scope.row.withdraw" type="primary" link size="small" @click.stop="handleMoney(scope.row)">
                <template #icon><Pointer /></template>
                提现
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
    <!-- 改派 -->
    <MoneyModal ref="modalRef" @success="handleQuery" />
  </div>
</template>

<script setup lang="ts">
import CommonApi from "@/api/common";
import { ref, onMounted, reactive } from "vue";
import { platFormEnum, channelEnum, platFormArr, channelArr } from "@/enums/common";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";
import ExportApi from "@/api/exports";
import MoneyApi from "@/api/money";
import MoneyModal from "./components/moneyModal.vue";

const queryFormRef = ref(ElForm);
const total = ref(0);
const supplierOptions = ref();
const modalRef = ref(null);
const cityOptions = ref();

const loading = ref(false);
// 点击供应商提现触
function handleMoney(row: any) {
  modalRef.value.edit(row);
}
function handleExprotItem(row: any) {
  let id = row.id;
  ExportApi.itemExport({ id }).then((res) => {
    let data = res.data;
    ElMessage.success("导出完毕");
    if (!data) {
      return;
    }
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(new Blob([data], { type: "application/vnd.ms-excel" }), fileName + ".xls");
    } else {
      let url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.ms-excel" }));
      let link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("download", "账单" + ".xlsx");
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  });
}

function handleExports() {
  if (!queryParams.billingTime) {
    ElMessage.error("请选择时间");
    return;
  }
  ExportApi.billExport(queryParams).then((res) => {
    let data = res.data;
    ElMessage.success("导出完毕");
    if (!data) {
      return;
    }
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(new Blob([data], { type: "application/vnd.ms-excel" }), fileName + ".xls");
    } else {
      let url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.ms-excel" }));
      let link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("download", "账单" + ".xlsx");
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  });
}

function handleSupplier() {
  ExportApi.supplier(queryParams).then((data) => {
    ElMessage.success("导出完毕");
    if (!data) {
      return;
    }
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(new Blob([data], { type: "application/vnd.ms-excel" }), fileName + ".xls");
    } else {
      let url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.ms-excel" }));
      let link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("download", "账单" + ".xlsx");
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  });
}

const handleChange = (file: any, title: any) => {
  uploadFileToServer(file, title);
};

const handleBeforeUpload = (file: any) => {
  // 在这里可以进行文件类型和大小的校验
  const isXLSX = file.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  if (!isXLSX) {
    ElMessage.error("只能上传xlsx文件!");
    return false;
  }
  return true;
};

const uploadFileToServer = async (file: any, title: any) => {
  try {
    const formData = new FormData();
    formData.append("file", file.raw);

    if (title == "罚款导入") {
      ExportApi.fkImport(formData).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
        }
      });
    }
    if (title == "扣款导入") {
      ExportApi.kkImport(formData).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
        }
      });
    }
    if (title == "补偿导入") {
      ExportApi.bcImport(formData).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
        }
      });
    }
    if (title == "作废导入") {
      ExportApi.zfImport(formData).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
        }
      });
    }
    if (title == "供应商导入") {
      ExportApi.supplierImport(formData).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
        }
      });
    }
  } catch (error) {
    console.error("上传文件时发生错误:", error);
  }
};

//罚款导入
function handleImportExcel(file) {
  handleImportXls(file, "/billing/fineImport", () => {
    setTimeout(() => {
      handleQuery();
    }, 1500);
  });
}

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);

// 选择表格的行菜单ID
const selectedMenuId = ref<number | undefined>();

// 查询菜单
function handleQuery() {
  loading.value = true;
  MoneyApi.getList(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}
import router from "@/router";
//点击明细触发
function handleDetail(row: any) {
  router.push(`/financeDetail?id=${row.id}`);
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

// 行点击事件
function handleRowClick(row: MenuVO) {
  // 记录表格选择的菜单ID，新增子菜单作为父菜单ID
  selectedMenuId.value = row.id;
}

onMounted(() => {
  handleQuery();
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.upload_box {
  display: flex;
  align-items: center;
  .upload-demo {
    margin: 0 4px;
  }
}
.red_rate {
  color: red;
}
.address_box {
  display: flex;
  flex-direction: column;
  align-items: left;
  justify-content: left;
  .item {
    display: flex;
    align-items: center;
    margin: 4px 0;
    .tag {
      width: 50px;
      height: 23px;
      text-align: center;
      color: orange;
      background: #fff8e6;
    }
    .tag2 {
      color: #3fd175;
      background: #e7faf0;
    }
  }
}
</style>
